package org.jeecg.modules.business.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.TokenUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.business.entity.BusHotel;
import org.jeecg.modules.business.entity.BusMarketMember;
import org.jeecg.modules.business.service.IBusHotelService;
import org.jeecg.modules.business.service.IBusMarketMemberService;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

/**
 * @Description: bus_market_member_info
 * @Author: jeecg-boot
 * @Date:   2023-03-02
 * @Version: V1.0
 */
@Api(tags="bus_market_member_info")
@RestController
@RequestMapping("/org.jeecg.modules.business/busMarketMember")
@Slf4j
public class BusMarketMemberController extends JeecgController<BusMarketMember, IBusMarketMemberService> {
	@Autowired
	private IBusMarketMemberService busMarketMemberService;
	 @Autowired
private IBusHotelService busHotelService;

	/**
	 * 分页列表查询
	 *
	 * @param busMarketMember
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "bus_market_member_info-分页列表查询")
	@ApiOperation(value="bus_market_member_info-分页列表查询", notes="bus_market_member_info-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<BusMarketMember>> queryPageList(BusMarketMember busMarketMember,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<BusMarketMember> queryWrapper = QueryGenerator.initQueryWrapper(busMarketMember, req.getParameterMap());
		Page<BusMarketMember> page = new Page<BusMarketMember>(pageNo, pageSize);
		IPage<BusMarketMember> pageList = busMarketMemberService.page(page, queryWrapper);
		if (pageList.getRecords().size() > 0) {
			pageList.getRecords().forEach(item -> {
				BusHotel hotle = busHotelService.getById(item.getHotelId());
				if (hotle != null) {
					item.setHotelName(hotle.getName());
				}
			});
		}
		return Result.OK(pageList);
	}

	 @ApiOperation(value="排除指定id列表查询", notes="排除指定id列表查询")
	 @GetMapping(value = "/getSelectList")
	 public Result<List<BusMarketMember>> queryList(String id,String hotelId) {
		 return  busMarketMemberService.getList(id,hotelId);
	 }
	/**
	 *   添加
	 *
	 * @param busMarketMember
	 * @return
	 */
	@AutoLog(value = "bus_market_member_info-添加")
	@ApiOperation(value="bus_market_member_info-添加", notes="bus_market_member_info-添加")
	//@RequiresPermissions("org.jeecg.modules.business:bus_market_member_info:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody BusMarketMember busMarketMember) {

		LoginUser user = TokenUtils.getAuthUser();
		if (user.getRelTenantIds() != null && !user.getRelTenantIds().equals("")) {
			busMarketMember.setTenantId(user.getRelTenantIds());
		} else {
			throw new JeecgBootException("当前登录人租户信息错误");
		}

		LambdaQueryWrapper<BusMarketMember> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(BusMarketMember::getLevel,busMarketMember.getLevel());
		queryWrapper.eq(BusMarketMember::getHotelId,busMarketMember.getHotelId());
		BusMarketMember one = busMarketMemberService.getOne(queryWrapper);
		if (one != null) {
			return Result.error("已存在相同等级！");
		}
		busMarketMemberService.save(busMarketMember);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param busMarketMember
	 * @return
	 */
	@AutoLog(value = "bus_market_member_info-编辑")
	@ApiOperation(value="bus_market_member_info-编辑", notes="bus_market_member_info-编辑")
	//@RequiresPermissions("org.jeecg.modules.business:bus_market_member_info:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody BusMarketMember busMarketMember) {
		LambdaQueryWrapper<BusMarketMember> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(BusMarketMember::getLevel,busMarketMember.getLevel());
		queryWrapper.ne(BusMarketMember::getId,busMarketMember.getId());
		BusMarketMember one = busMarketMemberService.getOne(queryWrapper);
		if (one != null) {
			return Result.error("已存在相同等级！");
		}
		busMarketMemberService.updateById(busMarketMember);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "bus_market_member_info-通过id删除")
	@ApiOperation(value="bus_market_member_info-通过id删除", notes="bus_market_member_info-通过id删除")
	//@RequiresPermissions("org.jeecg.modules.business:bus_market_member_info:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		busMarketMemberService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "bus_market_member_info-批量删除")
	@ApiOperation(value="bus_market_member_info-批量删除", notes="bus_market_member_info-批量删除")
	//@RequiresPermissions("org.jeecg.modules.business:bus_market_member_info:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.busMarketMemberService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "bus_market_member_info-通过id查询")
	@ApiOperation(value="bus_market_member_info-通过id查询", notes="bus_market_member_info-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<BusMarketMember> queryById(@RequestParam(name="id",required=true) String id) {
		BusMarketMember busMarketMember = busMarketMemberService.getById(id);
		if(busMarketMember==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(busMarketMember);
	}

	 /**
	  * 列表查询
	  *
	  * @param busMarketMember
	  * @param req
	  * @return
	  */
	 //@AutoLog(value = "会员信息-列表查询")
	 @ApiOperation(value="会员信息-列表查询", notes="会员信息-列表查询")
	 @GetMapping(value = "/queryList")
	 public Result<List<BusMarketMember>> queryList(BusMarketMember busMarketMember, HttpServletRequest req) {
		 QueryWrapper<BusMarketMember> queryWrapper = QueryGenerator.initQueryWrapper(busMarketMember, req.getParameterMap());
		 LoginUser user = TokenUtils.getAuthUser();
		 if(user.getRelTenantIds() != null && !user.getRelTenantIds().equals("")){
			 queryWrapper.eq("tenant_id",user.getRelTenantIds());
		 }
//		 queryWrapper.eq("del_flag",0);
		 List<BusMarketMember> list = busMarketMemberService.list(queryWrapper);
		 return Result.OK(list);
	 }

    /**
    * 导出excel
    *
    * @param request
    * @param busMarketMember
    */
    //@RequiresPermissions("org.jeecg.modules.business:bus_market_member_info:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, BusMarketMember busMarketMember) {
        return super.exportXls(request, busMarketMember, BusMarketMember.class, "bus_market_member_info");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("org.jeecg.modules.business:bus_market_member_info:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, BusMarketMember.class);
    }

}
